package com.yf1706.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.yf1706.bean.Role;
import com.yf1706.bean.User;
import com.yf1706.service.EmpService;
import com.yf1706.service.GrantService;
import com.yf1706.service.UserService;
import com.yf1706.tools.UserTools;
import com.yf1706.vo.FunctionTypeExVo;
import com.yf1706.vo.Information;
import com.yf1706.vo.RoleFunctionsVo;
import com.yf1706.vo.UserEmpRoleVO;

@Controller
@RequestMapping("grant")
//添加session
@SessionAttributes({ "user", "funIds" })
public class GrantController {

	@Autowired
	private GrantService gtService;
	@Autowired
	private EmpService es;
	@Autowired
	private UserService us;

	@RequestMapping("getAllRoles")
	@ResponseBody
	public List<Role> getAllRole() {
		System.out.println(Arrays.toString(gtService.getAllRoles().toArray()));
		return gtService.getAllRoles();
	}

	@RequestMapping("addNewRole")
	@ResponseBody
	public Information addNewRole(String roleName) {
		try {
			gtService.addNewRole(roleName);
			return Information.SUCCESS;
		} catch (Exception e) {
			// TODO: handle exception
			return Information.failure(e.getMessage());
		}
	}

	@RequestMapping("deleteRole")
	@ResponseBody
	public Information deleteRole(int roleId) {
		try {
			gtService.deleteRole(roleId);
			return Information.SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return Information.failure(e.getMessage());
		}
	}

	@RequestMapping("getFunctionTypeExs")
	@ResponseBody
	public List<FunctionTypeExVo> getFunctionTypeExs(int roleId) {
		return gtService.getFunctionTypeExs(roleId);
	}

	@PostMapping("updateRole")
	@ResponseBody
	public Information updateRole(String roleId,String roleName) {
		try {
			System.out.println(roleId+":"+roleName);
			gtService.updateRole(Integer.parseInt(roleId), roleName);
			return Information.SUCCESS;
		} catch (Exception e) {
			// TODO: handle exception
			return Information.failure(e.getMessage());
		}
	}
	
	@RequestMapping("findUserEmpRoleVO")
	@ResponseBody
	public List<UserEmpRoleVO> findUserEmpRoleVO(String roleId) {
		System.out.println("进入findUserEmpRoleVO");
		System.out.println(gtService.findUserEmpRoleVO(Integer.parseInt(roleId)).size());
		return gtService.findUserEmpRoleVO(Integer.parseInt(roleId));
	}

	@PostMapping("grantRole")
	@ResponseBody
	public Information grantRole(RoleFunctionsVo vo) {
		try {
			System.out.println(vo);
			gtService.grantRole(vo.getRoleId(), vo.getFuncIds());
			return Information.SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return Information.failure(e.getMessage());
		}
	}
	
	@RequestMapping("addUserForRole")
	@ResponseBody
	public Information addUserForRole(String jsonStr) {
		try {
			Map map = JSON.parseObject(jsonStr);  
	        System.out.println("这个是用JSON类的parseObject来解析JSON字符串!!!");  
	        for (Object obj : map.keySet()){  
	            System.out.println("key为："+obj+"值为："+map.get(obj));  
	        }
	        int roleId=Integer.parseInt((String) map.get("roleId"));
	        List userIds=(List)map.get("userIds");
	        gtService.grantUser(userIds, roleId);
			return Information.SUCCESS;
		}catch (Exception e) {
			e.printStackTrace();
			return Information.failure(e.getMessage());
		}
	}

	@RequestMapping("toLogin")
	public String toLogin() {
		return "zxy/login";
	}
	
	@RequestMapping("getUserByRoleId")
	@ResponseBody
	public Information getUserByRoleId(int roleId) {
		try {
			Map<String,Object> map=new HashMap<>();
			Information info=new Information(true,map);
			return info;
		}catch (Exception e) {
			// TODO: handle exception
			return Information.failure(e.getMessage());
		}
	}
	
	@RequestMapping("login")
	@ResponseBody
	public Information login(String userId, String psw, Model model) {
		try {
			User user = gtService.login(UserTools.userIdUnformat(userId), psw);
			if (user != null) {
				model.addAttribute("user", user);
				model.addAttribute("funIds", gtService.getFunIdsByUserId(user.getUserId()));
				System.err.println(gtService.getFunIdsByUserId(user.getUserId()));
				return Information.SUCCESS;
			}
			return Information.failure("账户名密码不正确或权限不足");
		} catch (Exception e) {
			// TODO: handle exception
			return Information.failure(e.getMessage());
		}

	}
}
